<template>
  <el-row>
    <el-col :span="24">
      <el-form ref="searchForm" class="el-form-col search-form" :model="search">
        <el-row :gutter="15">
          <el-col :lg="4" :md="6">
            <el-form-item prop="orderNo">
              <el-input v-model="search.orderNo" type="textarea" :rows="4" clearable placeholder="关联订单号  (支持多单查询，用英文逗号分隔)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item prop="outWarehouseNo" style="position: relative">
              <el-input v-model="search.outWarehouseNo" type="textarea" :rows="4" clearable placeholder="出库单号  (支持多单查询，用英文逗号分隔)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item prop="consigneName">
              <el-input v-model="search.consigneName" placeholder="客户姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6" class="hidden-lg-only hidden-xl-only">
            <el-form-item>
              <el-row class="search-btns" :gutter="5">
                <el-col :span="8">
                  <el-button type="primary" :loading="searchLoading" plain native-type="submit" @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                </el-col>
                <el-col :span="8">
                  <el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                </el-col>
                <el-col :span="8">
                  <el-button @click="resetFields" plain>重置</el-button>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item prop="consignePhone">
              <el-input v-model="search.consignePhone" placeholder="客户电话"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item prop="cargoOwner">
              <el-input v-model="search.cargoOwner" placeholder="商家名称"></el-input>
            </el-form-item></el-col>
          <el-col :lg="4" :md="6" class="hidden-md-only hidden-sm-only">
            <el-form-item>
              <el-row class="search-btns" :gutter="5">
                <el-col :span="8">
                  <el-button type="primary" :loading="searchLoading" plain native-type="submit" @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                </el-col>
                <el-col :span="8">
                  <el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                </el-col>
                <el-col :span="8">
                  <el-button @click="resetFields" plain>重置</el-button>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item prop="orderType">
              <el-select v-model="search.orderType" placeholder="关联订单类型">
                <el-option v-for="item in followTypeArr"
                           :key="item.value"
                           :label="item.text"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item prop="outType">
              <el-select v-model="search.outType" placeholder="仓储类型">
                <el-option v-for="item in tempArr"
                           :key="item.value"
                           :label="item.text"
                           :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item  prop="ocmStartTime">
              <el-date-picker type="date"
                              placeholder="生成开始时间"
                              v-model="search.ocmStartTime"
                              value-format="yyyy-MM-dd"
                              style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item prop="ocmEndTime">
              <el-date-picker type="date"
                              placeholder="生成结束时间"
                              v-model="search.ocmEndTime"
                              value-format="yyyy-MM-dd"
                              style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <div v-show="isOpen">
            <el-col :lg="4" :md="6" v-if="state == 2">
              <el-form-item prop="distributionStartTime">
                <el-date-picker type="date"
                                placeholder="分配开始时间"
                                v-model="search.distributionStartTime"
                                value-format="yyyy-MM-dd"
                                style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6" v-if="state == 2">
              <el-form-item prop="distributionEndTime">
                <el-date-picker type="date"
                                placeholder="分配结束时间"
                                v-model="search.distributionEndTime"
                                value-format="yyyy-MM-dd"
                                style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </div>
        </el-row>
      </el-form>
    </el-col>
    <el-col :span="24">
      <BasicTable
        ref="BasicTable"
        url="/ms-warehouse-order/outbound-distribution/get-distribution-listCD"
        :tabs="tabs"
        :columns="columns">
        <template slot="preColumn">
          <el-table-column prop="orderNo" fixed="left" width="155px" align="center" label="关联单号">
            <template slot-scope="scope">
              <a href="javascript:;" class="blue-color" @click="showOrder(scope.row)">{{ scope.row.orderNo }}</a>
            </template>
          </el-table-column>
          <el-table-column prop="outWarehouseNo" fixed="left" width="175px" align="center" label="出库单号">
            <template slot-scope="scope">
              <a href="javascript:;" class="blue-color" @click="showOrder(scope.row)">{{ scope.row.outWarehouseNo }}</a>
            </template>
          </el-table-column>
        </template>
        <template slot="column">
          <el-table-column fixed="right" width="100px" align="center" label="操作" v-if="state == 1">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="showAllocation(scope.row)"
              >
                库位分配
              </el-button>
            </template>
          </el-table-column>
          <el-table-column fixed="right" width="100px" align="center" label="操作" v-if="state == 2">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="showAllocation(scope.row)"
              >
                详情
              </el-button>
            </template>
          </el-table-column>
        </template>
      </BasicTable>
    </el-col>
  </el-row>
</template>
<script>
import {filterDataBaseDictionary, unixTimestampFilter} from '@/filters'
import {copy} from '@/utils'
import {getCargoSourceType, getSubDictListOfParentCode} from '@/utils/selectOptions'
export default {
  name: 'outputWarehouseAllocationCdIndex',
  data() {
    let columns = [
      {
        label: '客户姓名',
        width: '80px',
        data: 'consigneName'
      },
      {
        label: '客户电话',
        width: '100px',
        data: 'consignePhone'
      },
      {
        label: '关联订单类型',
        data: 'orderType',
        width: '120px',
        render(data, full) {
          return filterDataBaseDictionary(data)
        }
      },
      {
        label: '出库仓',
        data: 'warehouse',
        render(data, full) {
          return filterDataBaseDictionary(data)
        }
      },
      {
        label: '仓储类型',
        data: 'outTypeCode',
        width: '100px',
        render(data, full) {
          return filterDataBaseDictionary(data)
        }
      },
      {
        label: '商家名称',
        data: 'cargoOwner'
      },
      {
        label: '总体积',
        width: '82px',
        data: 'totalVolume'
      },
      {
        label: '总包件',
        width: '82px',
        data: 'totalPackage'
      },
      {
        label: '总重量',
        width: '80px',
        data: 'totalWeight'
      }
    ]
    let unAssignedColumns = copy(columns, true)
    unAssignedColumns.splice(unAssignedColumns.length, 0, {
      label: '生成时间',
      data: 'foundDate',
      render(data, type, full, meta) {
        return unixTimestampFilter(data, 'YYYY-MM-DD HH:mm')
      }
    })
    let assignedColumns = copy(columns, true)
    assignedColumns.splice(9, 0, {
      label: '分配人',
      data: 'distributionUserName'
    },
    {
      label: '分配时间',
      data: 'distributionDate',
      render(data, type, full, meta) {
        return unixTimestampFilter(data, 'YYYY-MM-DD HH:mm')
      }
    })
    return {
      followTypeArr: [], // 关联订单类型
      state: 1,
      isOpen: false,
      visible: false,
      searchLoading: false,
      search: {
        orderNo: '',
        outWarehouseNo: '',
        warehouseId: '',
        cargoOwner: '',
        orderType: '',
        outType: '',
        consigneName: '',
        consignePhone: '',
        ocmStartTime: '',
        ocmEndTime: '',
        distributionStartTime: '',
        distributionEndTime: ''
      },
      mark: 0,
      columns,
      tabs: [
        {
          label: '未分配',
          callback: (params) => {
            this.state = 1
            params.tabState = 1
          },
          columns: unAssignedColumns
        },
        {
          label: '已分配',
          callback: (params) => {
            this.state = 2
            params.tabState = 2
          },
          columns: assignedColumns
        }
      ],
      tempArr: [],
      item: {}
    }
  },
  created() {
    // 关联订单类型：通用字典接口查
    getSubDictListOfParentCode('STORAGEORDERTYPE').then(arr => {
      if (Array.isArray(arr)) {
        this.followTypeArr = arr
      }
    })
    getCargoSourceType().then(arr => {
      this.tempArr = arr
    })
  },
  methods: {
    // 搜索
    searchFunc(search) {
      const basicTable = this.$refs.BasicTable
      if (basicTable) {
        let params = copy(search)
        this.searchLoading = true
        basicTable.search(params).finally(_ => {
          this.searchLoading = false
        })
      }
    },
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    // 表格刷新
    refresh() {
      this.searchFunc(this.search)
    },
    showOrder(data) {
      this.$router.push({path: '/ss-oms/outputWarehouse/warehousing-order/show/' + data.id})
    },
    // 跳转详情
    showAllocation(data) {
      this.$router.push({path: '/ss-oms/outputWarehouse/warehouse-allocation-cd/show/' + data.id, query: {distributionStart: data.distributionStart, state: this.state}})
    }
  }
}
</script>
